package com.qianfeng.springcloud.order.controller;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qianfeng.springcloud.order.feign.AddressOpenfeign;
import com.qianfeng.springcloud.order.pojo.Order;
import com.qianfeng.springcloud.order.pojo.RemoteAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * Created by Jackiechan on 2022/7/6 20:55
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private AddressOpenfeign addressOpenfeign;

    @Autowired
    public void setAddressOpenfeign(AddressOpenfeign addressOpenfeign) {
        this.addressOpenfeign = addressOpenfeign;
    }

    @GetMapping("/info/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallback")
    public Order findById(@PathVariable Integer id) {
        Order order =   new Order();
        order.setId(id);
        order.setPrice(new Random().nextDouble());
        order.setItemName("打气筒"+id);
        //中间的地址就是我们要请求的服务的名字,需要在创建resttemplate的时候添加LoadBalanced注解
        RemoteAddress remoteAddress = addressOpenfeign.findById(id);
        order.setRemoteAddress(remoteAddress);
        return order;
    }

    @GetMapping("/test1")
    public String test1(String name) {
        return addressOpenfeign.test1(name);
    }

    @GetMapping("/test2")
    public String test2(String name,int age) {
        return addressOpenfeign.test2(name, age);
    }


    @PostMapping("/test3")
    public RemoteAddress test3(@RequestBody RemoteAddress address) {
        return addressOpenfeign.test3(address);
    }
    @PostMapping("/test4")
    public RemoteAddress test4( RemoteAddress address) {
        return addressOpenfeign.test3(address);
    }


    public Order findByIdFallback(Integer id) {
        Order order =   new Order();
        order.setId(-id);
        order.setPrice(new Random().nextDouble());
        order.setItemName("降级的数据"+id);
        RemoteAddress remoteAddress = new RemoteAddress();
        remoteAddress.setId(-100);
        remoteAddress.setCity("city");
        remoteAddress.setName("name====?");
        order.setRemoteAddress(remoteAddress);
        return order;
    }
}
